/**
 * 
 */
package net.mikoo.seals.biz.common.helper;

import java.util.ArrayList;
import java.util.List;

import net.mikoo.seals.model.Organization;


public class OrgsHelper {
	
	public static List<Organization> convertToTree(List<Organization> childs, Long primaryId, int level) {
		
		List<Organization> organizations = new ArrayList<Organization>();
		
		String str = "";
		int index = 0;
		for (Organization orgs : childs) {
			
			if (orgs.getParentId().equals(primaryId)) {
				index++;
				List<Organization> subs = loadByPrimaryId(childs, orgs.getOrganizationId());
				for (int i = 0; i < level; i++) {
					
					if (i + 1 == level) {
						
						if (index == subs.size()) {
							str = str + "\u0008 \u0008 \u0008└─";
						} else {
							str = str + "\u0008 \u0008 \u0008├─";
						}
					} else {
						
						str = str + "\u0008 \u0008 \u0008│";
						
					}
					
				}
				orgs.setName(str + " " + orgs.getName());
				//Organization.setOrganizationName(Organization.getOrganizationName());
				orgs.setChildren(subs);
				orgs.setLevel(level);
				organizations.add(orgs);
				str = "";
				if (subs.size() > 0) {
					organizations.addAll(convertToTree(childs, orgs.getOrganizationId(), level + 1));
				}
			}
		}
		
		return organizations;
		
	}
	
	
	public static List<Organization> convertToTree(List<Organization> orgs, Long primaryId) {
		
		List<Organization> suOrganizations = loadByPrimaryId( orgs,  primaryId) ;
		if(suOrganizations != null && !suOrganizations.isEmpty()) {
			
			for (Organization org : suOrganizations) {
				
				List<Organization> childs =  convertToTree(orgs, org.getOrganizationId());
				org.setChildren(childs);
			}
			
		}
		
		return suOrganizations;
	}
	
	/**
	 * 查询子类
	 * @param Organizations
	 * @param primaryId
	 * @return
	 */
	public static List<Organization> loadByPrimaryId(List<Organization> Organizations, Long primaryId) {
		List<Organization> childs = new ArrayList<Organization>();
		
		for (Organization Organization : Organizations) {
			if (Organization.getParentId() == primaryId) {
				childs.add(Organization);
			}
		}
		return childs;
	}
	
}
